package com.ruoyi.web.controller.production.fertilizer;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.fertilizer.FertilizerName;
import com.ruoyi.production.service.fertilizer.IFertilizerNameService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 化肥名称Controller
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@RestController
@RequestMapping("/production/fertilizerName")
public class FertilizerNameController extends BaseController
{
    @Autowired
    private IFertilizerNameService fertilizerNameService;

    /**
     * 查询化肥名称列表
     */
    @PreAuthorize("@ss.hasPermi('production:fertilizerName:list')")
    @GetMapping("/list")
    public TableDataInfo list(FertilizerName fertilizerName)
    {
        startPage();
        List<FertilizerName> list = fertilizerNameService.selectFertilizerNameList(fertilizerName);
        return getDataTable(list);
    }

    /**
     * 导出化肥名称列表
     */
    @PreAuthorize("@ss.hasPermi('production:fertilizerName:export')")
    @Log(title = "化肥名称", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FertilizerName fertilizerName)
    {
        List<FertilizerName> list = fertilizerNameService.selectFertilizerNameList(fertilizerName);
        ExcelUtil<FertilizerName> util = new ExcelUtil<FertilizerName>(FertilizerName.class);
        return util.exportExcel(list, "化肥名称数据");
    }

    /**
     * 获取化肥名称详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:fertilizerName:query')")
    @GetMapping(value = "/{fertilizerNameId}")
    public AjaxResult getInfo(@PathVariable("fertilizerNameId") Long fertilizerNameId)
    {
        return AjaxResult.success(fertilizerNameService.selectFertilizerNameById(fertilizerNameId));
    }

    /**
     * 新增化肥名称
     */
    @PreAuthorize("@ss.hasPermi('production:fertilizerName:add')")
    @Log(title = "化肥名称", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FertilizerName fertilizerName)
    {
        try {
            return toAjax(fertilizerNameService.insertFertilizerName(fertilizerName));
        }catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return AjaxResult.error("名称已存在！");
            }else return error("失败!");
        }
    }

    /**
     * 修改化肥名称
     */
    @PreAuthorize("@ss.hasPermi('production:fertilizerName:edit')")
    @Log(title = "化肥名称", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FertilizerName fertilizerName)
    {
        try {
            return toAjax(fertilizerNameService.updateFertilizerName(fertilizerName));
        }catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return AjaxResult.error("名称已存在！");
            }else return error("失败!");
        }
    }

    /**
     * 删除化肥名称
     */
    @PreAuthorize("@ss.hasPermi('production:fertilizerName:remove')")
    @Log(title = "化肥名称", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fertilizerNameIds}")
    public AjaxResult remove(@PathVariable Long[] fertilizerNameIds)
    {
        return toAjax(fertilizerNameService.deleteFertilizerNameByIds(fertilizerNameIds));
    }
}
